package org.alex.sample.dao.Impl;

import java.util.Iterator;
import java.util.List;

import org.alex.sample.dao.ExamTestDAO;
import org.alex.sample.model.ExamTest;
import org.alex.sample.page.PagerModel;
import org.alex.sample.page.SystemContext;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
@Transactional
public class ExamTestDAOImpl extends BaseHibernateDAO<ExamTest> implements ExamTestDAO {
	
	@Override
	public List<ExamTest> findAllExamTest() throws Exception {
		String hql = "FROM ExamTest";
		List<ExamTest> examTest = this.find(hql);
		return examTest;
	}

	@Override
	public ExamTest findExamTestById(Integer id) throws Exception {
		ExamTest examTest = this.get(id);
		return examTest ;
	}

	@Override
	public void createExamTest(ExamTest examTest) throws Exception {
		this.save(examTest);
	}

	@Override
	public void editExamTest(ExamTest examTest) throws Exception {
		this.update(examTest);
	}

	@Override
	public void deleteExamTest(Integer id) throws Exception {
		this.deleteByKey(id);
	}
	
	@SuppressWarnings("unchecked")
	public PagerModel<ExamTest> findAllByPage() throws Exception{
		PagerModel<ExamTest> page = new PagerModel<ExamTest>();
		int offset = SystemContext.getOffset(); //偏移量
		int pageSize = SystemContext.getPageSize(); //每页的记录数
		//使用hibernate完成分页
		List<ExamTest> tempDates = (List<ExamTest>) this.getSession().createQuery("from ExamTest")
				.setFirstResult(offset).setMaxResults(pageSize).list();
		page.setDates(tempDates);
		page.setOffset(offset);
		page.setPageSize(pageSize);
		
		//获取总记录数
		long totalSize = (long)this.getSession().createQuery("select count(*) from ExamTest").uniqueResult();
		page.setTotalSize(totalSize);
		return page;
	}

	@Override
	public Iterator<ExamTest> iterate(String queryString, Object[] values) {
		// TODO Auto-generated method stub
		return null;
	}

}
